JavaScript

Check Console for Output

For Explanation Click Here

Array and Objects accessing few Method Examples

1.Write a JavaScript code to add an element to the end of an array using the 
push method. Example: [1, 2, 3] → [1, 2, 3, 4] var arr = [1, 2, 3]; arr.push(4); console.log(arr);
// Output: [1, 2, 3, 4] 2.Write a JavaScript code to remove the last element from an array using the pop
method. Example: [1, 2, 3, 4] → [1, 2, 3] var arr = [1, 2, 3, 4]; arr.pop(); console.log(arr);
// Output: [1, 2, 3] 3.Write a JavaScript code to add an element to the beginning of an array using
the unshift method. Example: [2, 3, 4] → [1, 2, 3, 4] var arr = [2, 3, 4]; arr.unshift(1); console.log(arr);
// Output: [1, 2, 3, 4] 4.Write a JavaScript code to remove the first element from an array using the
shift method. Example: [1, 2, 3, 4] → [2, 3, 4] var arr = [1, 2, 3, 4]; arr.shift(); console.log(arr);
// Output: [2, 3, 4] 5.Write a JavaScript code to convert an array into a string using the join
method. Example: [1, 2, 3] → "123" var arr = [1, 2, 3]; var x = arr.join(""); console.log(x);
// Output: "123" Choose the Correct Answer: 1. What will be the output of [1, 2, 3, 4].pop()? A) [1, 2, 3, 4] B) [1, 2, 3] ✓ C) [2, 3, 4] D) Error // Correct Answer: B) [1, 2, 3] // pop() removes the last element from the array 2. What will be the output of [1, 2, 3].push(4)? A) [1, 2, 3] B) [1, 2, 3, 4] ✓ C) [4, 1, 2, 3] D) Error // Correct Answer: B) [1, 2, 3, 4] // push(4) adds 4 to the end of the array 3. What will be the output of [1, 2, 3, 4].shift()? A) [1, 2, 3, 4] B) [2, 3, 4] ✓ C) [1, 2, 3] D) Error // Correct Answer: B) [2, 3, 4] // shift() removes the first element from the array 4. What will be the output of [2, 3, 4].unshift(1)? A) [2, 3, 4] B) [1, 2, 3, 4] ✓ C) [1, 3, 4] D) Error // Correct Answer: B) [1, 2, 3, 4] // unshift(1) adds 1 to the beginning of the array 5. What will be the output of [1, 2, 3].join("")? A) 123 ✓ B) [1, 2, 3] C) 1 2 3 D) Error // Correct Answer: A) 123 // join("") combines all elements into a string with no separator